import { connect } from 'dva';
import uuid from 'es6-uuid';
import moment from 'moment/moment';
import PropTypes from 'prop-types';
import React, { PureComponent } from 'react';

import { ModalC, TableC } from '@/src/components';
import { findTitleByValue } from '@/src/utils/utils';

const isBooleanArr = [
  { value: '0', title: '是' },
  { value: '1', title: '否' },
];
const statisticsType = [
  { value: 'ZCGLJH', title: '资产管理计划' },
  { value: 'WW', title: '委外投资' },
  { value: 'SMJJ', title: '私募基金' },
];
@connect(({ positionThroughQueryNewM, globalM, loading }) => ({
  positionThroughQueryNewM,
  globalM,
  loading,
}))
class DataLogs extends PureComponent {
  static defaultProps = {
    deadline: undefined,
    showLog: () => {},
  };

  state = {
    logData: [],
    logColumns: [
      { key: 'expirationDate', title: '报表日期' },
      { key: 'tradeType', title: '资产类型' },
      { key: 'tradingCode', title: '证券代码' },
      { key: 'tradingName', title: '证券全称' },
      { key: 'isValuation', title: '是否提供当日估值解析' },
      { key: 'valuationDate', title: '最新估值表数据日期' },
    ],
  };

  componentDidMount() {
    this.getDateLogs(this.props.deadline);
  }

  getDateLogs = (deadline) => {
    this.props.dispatch({
      type: 'limitThroughReportNewM/queryLogList',
      payload: {
        expirationDate: moment(deadline).format('YYYY-MM-DD'),
      },
      callback: (data) => {
        if (data) {
          data?.dataSource?.map((item) => {
            item.id = uuid(16);
          });
        }
        this.setState({
          logData: data || {},
        });
      },
    });
  };

  showLogTable = () => {
    const { loading } = this.props;
    const { logColumns = [], logData } = this.state;
    logColumns.map((item) => {
      if (item.key == 'tradeType') {
        item.render = (text) => findTitleByValue(text, statisticsType);
      }
      if (item.key == 'isValuation') {
        item.render = (text) => findTitleByValue(text, isBooleanArr);
      }
      if (item.key == 'expirationDate') {
        item.type = 'date';
      }
    });
    return (
      <div style={{ marginTop: '20px' }}>
        <TableC
          loading={loading.effects['limitThroughReportNewM/queryLogList'] == true}
          columns={logColumns}
          dataSource={logData.dataSource || []}
          hidePage={true}
          scroll={{ y: 300 }}
          rowKey='id'
        />
      </div>
    );
  };

  render() {
    const { logData } = this.state;
    const { showLog } = this.props;
    const { deadline } = this.props;
    const props = {
      visible: true,
      onOk: () => {
        showLog(false);
      },
      onCancel: () => {
        showLog(false);
      },
      title: '数据日志',
      footer: null,
      height: 500,
      width: 1200,
      maskClosable: false,
    };
    return (
      <div>
        <ModalC {...props}>
          <div>
            <div>
              跑批结束时间：
              {logData.runEndDateTime ? moment(logData.runEndDateTime).format('YYYY-MM-DD HH:mm:ss') : '--'}
            </div>
            {logData.statistics &&
              logData.statistics.map((item, key) => (
                <div key={key} style={{ lineHeight: '24px' }}>
                  {deadline?.format('YYYY-MM-DD')}
                  日存续
                  {findTitleByValue(item.type, statisticsType)}共{item.count || 0}
                  只，当日估值解析已导入
                  {item.successCount || 0}
                  只，尚未导入
                  {item.failCount || 0}
                  只。
                </div>
              ))}
            {this.showLogTable()}
          </div>
        </ModalC>
      </div>
    );
  }
}
DataLogs.propTypes = {
  deadline: PropTypes.object,
  showLog: PropTypes.func,
};
export default DataLogs;
